﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Linq;
using CommunitySystem.PageView;

namespace CommunitySystem
{
    public partial class CandidatorInfoControl : AsyncCtrl, IReturnListenerc
	{
        private JinShengService.JinShengServiceClient _client = null;
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        public CandidatorInfoControl(JinShengService.JinShen_MainInfo mainInfo)
		{
			// 为初始化变量所必需
			InitializeComponent();
            _MainInfo = mainInfo;
            _client = new JinShengService.JinShengServiceClient();
            _client.GetDemocraEvalutionCompleted += (sender, e) =>
            {
                var eval =  e.Result.FirstOrDefault();
                TextBlock tb = new TextBlock() { Text = "应参加测评人数:", Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = eval.TotalCount + "", VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = "实际加测评人数:", Margin = new Thickness(80, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = eval.Realcount + "", VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                int index = 1;
                foreach (var Evaldetail in eval.Details)
                {
                    var detail = _MainInfo.Details.Where(c => c.EmpBasicID == Evaldetail.EmpID).FirstOrDefault();
                    if (detail != null)
                    {
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 10, 10, 0), Height = 30, VerticalAlignment = System.Windows.VerticalAlignment.Top, DataContext = detail, Tag = Evaldetail };
                        tb = new TextBlock() { Text = detail.Name, Margin = new Thickness(0, 0, 10, 0), Width=35, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        if (index % 2 == 1)
                            sp.Background = new SolidColorBrush(Colors.LightGray);
                        index++;
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = detail.IsPoGe == true ? "破格提拔" : "未破格提拔", Margin = new Thickness(0, 0, 10, 0), Width=80 ,VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "同意票数:" + Evaldetail.AgreeCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "不同意票数:" + Evaldetail.DisAgreeCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "弃权票数:" + Evaldetail.GiveUpCount, Margin = new Thickness(0, 0, 5, 0), Width = 80, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        tb = new TextBlock() { Text = "不同意率:" + Evaldetail.UnAgreeRate + "%", Margin = new Thickness(0, 0, 5, 0), Width = 110, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        sp.Children.Add(tb);
                        detailsInfo.Children.Add(sp);
                    }
                }
                if (_MainInfo.PromotType == "竞争上岗")
                {
                    _client.GetCompetSpeechMainAsync("MainID=" + mainInfo.ID);
                }
                else
                {
                    _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + _MainInfo.ID);
                }

            };



            if (_MainInfo.PromotType == "竞争上岗")
            {
                _client.GetCompetSpeechMainCompleted += (sender, e) =>
                {
                    foreach (var detail in e.Result.FirstOrDefault().Details)
                    {
                        var sp = detailsInfo.Children.Where(c => ((c as StackPanel).DataContext as JinShengService.JinShen_DetailInfo).EmpBasicID == detail.EmpID).FirstOrDefault() as StackPanel;
                        var hl = new HyperlinkButton() { Content = detail.realName, Width = 120, DataContext = detail, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                        hl.Click += new RoutedEventHandler(hl_Click);
                        sp.Children.Add(hl);
                    }
                    _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + _MainInfo.ID);
                };
            }
           

            _client.GetJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
            {
                var chooseMain = e.Result.FirstOrDefault();
                var rate = chooseMain.FilterRate;
                foreach (UIElement ui in detailsInfo.Children)
                {
                    StackPanel panel = ui as StackPanel;
                    var detail = panel.DataContext as JinShengService.JinShen_DetailInfo;
                    var Democraticevaluation_Detail = panel.Tag as JinShengService.JinShen_Democraticevaluation_Detail;
                    var Ispass = chooseMain.Details.Where(c => c.EmpID == detail.EmpBasicID).FirstOrDefault();
                    if (Ispass != null)
                        continue;
                    if (Democraticevaluation_Detail.UnAgreeRate >= rate)
                    {
                        panel.Children.Add(new TextBlock() { Text = "民主测评未通过", VerticalAlignment = System.Windows.VerticalAlignment.Center, Foreground = new SolidColorBrush(Colors.Red) });
                    }
                    else
                    {
                        panel.Children.Add(new TextBlock() { Text = "会议讨论未通过", VerticalAlignment = System.Windows.VerticalAlignment.Center, Foreground = new SolidColorBrush(Colors.Red) });
                    }

                }
            };

            _client.GetDemocraEvalutionAsync("MainID=" + mainInfo.ID);
            _client.DownLoadFileCompleted += (sender, e) =>
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    string serverpath = _client.InnerChannel.Via.AbsoluteUri.Replace(_client.InnerChannel.Via.AbsolutePath, "");
                    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + e.Result, UriKind.RelativeOrAbsolute), "_blank");
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("文件被破坏");
                }
            };
		}

        void hl_Click(object sender, RoutedEventArgs e)
        {
            var hlb = sender as HyperlinkButton;
            JinShengService.JinShen_CompetSpeech_Detail detail = hlb.DataContext as JinShengService.JinShen_CompetSpeech_Detail;
            _client.DownLoadFileAsync("file", detail.GuidName, detail.realName);
        }

        public void BeginReturn()
        {
            
        }

        private void ImageButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            KaoChaControl control = new KaoChaControl(_MainInfo) { Nav = this.Nav };
            Nav.ShowNextPage("考察管理", control as AsyncCtrl);
        }
    }
}